package com.traffic.between.service;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.traffic.between.config.BetweenProperties;



public class HuaLeiHuiServiceImpl {
	private static Logger logger = Logger.getLogger(HuaLeiHuiServiceImpl.class);
	public static HuaLeiHuiServiceImpl service = new HuaLeiHuiServiceImpl();
	
	private static String HOST_ORDER_PATH = BetweenProperties.HUALH_ORDER_URL;
	private static String HOST_ORDER_CHANNELID = BetweenProperties.HUALH_ORDER_CHANNELID;
	private static String HOST_ORDER_KEY = BetweenProperties.HUALH_ORDER_KEY;
	
	
	public String order(String productbingdingId, String phone, String cstmOrderNo, String notifyUrl) {
		String result = "";

		String channelId = HOST_ORDER_CHANNELID;//花蕾荟平台提供

		String key = HOST_ORDER_KEY;//花蕾荟平台提供
	
		String waitSig = phone + channelId + productbingdingId + cstmOrderNo + key;
		// 签名
		String sign = getMD5(waitSig.trim());
		
		 List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
	     pairList.add(new BasicNameValuePair("productbingdingId", productbingdingId));
	     pairList.add(new BasicNameValuePair("phone", phone));
	     pairList.add(new BasicNameValuePair("privateChannel", cstmOrderNo));//订单号
	     pairList.add(new BasicNameValuePair("notifyUrl", notifyUrl));
	     pairList.add(new BasicNameValuePair("channelId", channelId));
	     pairList.add(new BasicNameValuePair("sign", sign));
	       
	
	     logger.info("between- 花蕾荟流量订购请求参数: " + pairList);
		HttpPost httpPost = new HttpPost(HOST_ORDER_PATH);
		CloseableHttpClient client = HttpClients.createDefault();

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			logger.info("between- 花蕾荟流量订购请求时的响应statusCode: " + statusCode);

			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity, "utf-8");
				logger.info("between- 花蕾荟流量订购请求时的响应数据是: " + result);
			}
		} catch (Exception e) {
			logger.info("between- 花蕾荟流量订购时请求异常: " + e);
		}

		return result;
	}

	
	
	
	/**
	 * md5加密 ，用于生成sign
	 * 
	 * @param message
	 * @return
	 */
	public static String getMD5(String message) {
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("UTF-8"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return md5StrBuff.toString().toLowerCase();// 字母小写
	}
	
}
